<template>
  <div>
    <h1>修改图书</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" style="width: 300px" class="demo-ruleForm">

      <el-form-item label="书名" prop="name" >
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>



      <el-form-item label="作者" prop="author">
        <el-input v-model="ruleForm.author"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="出版社" prop="publish">
        <el-input v-model="ruleForm.publish"></el-input>
      </el-form-item>
      <el-form-item label="页数" prop="pages">
        <el-input v-model.number="ruleForm.pages" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item hidden="hidden"  prop="id" >
        <el-input v-model="ruleForm.id" readonly></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "UpdateBook",
  created() {
    console.log(this.$route.query)
  },
  //
  data() {
    return {
      ruleForm: {
        id:this.$route.query.id,
        name: this.$route.query.name,
        author: this.$route.query.author,
        publish:this.$route.query.publish,
        pages:this.$route.query.pages,
        price:this.$route.query.price
      },
      rules: {
        name: [
          { required: true, message: '请输入书籍名字', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ],
        author: [
          { required: true, message: '请输入作者名字', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入书本价格', trigger: 'blur' },
          { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的格式,可保留两位小数', trigger: 'blur' }
        ],
        publish: [
          { required: true, message: '请输入出版社名字', trigger: 'blur' }
        ],
        pages: [
          { required: true, message: '请输入书籍页数', trigger: 'blur' },
          { pattern: /^[1-9][0-9]*$/, message: '正整数', trigger: 'blur'}

        ],
      }
    };
  },
  //
  methods: {
    // 修改成功
    addSuccess() {
      this.$message({
        message: '修改成功',
        type: 'success'
      });
    },
    // 修改失败
    addFailure() {
      this.$message({
        showClose: true,
        message: '修改失败',
        type: 'error'
      });
    },
    // 提交表单
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.put("/book/updateBook",_this.ruleForm).then(function (r) {
            if(r.data === 'success'){
              _this.addSuccess()
            }else {
              _this.addFailure()
            }
          })
        } else {
          _this.addFailure()
          return false;
        }
      });
    },

    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  }


}
</script>

<style scoped>
h1{
  text-align: center;
}
.el-form{
  margin:0 auto;
}
</style>

